import os
from tkinter import Frame, Button
from tkinter import W,E,S,N

from CommonOperations.project_root_path import project_root_path
from CommonOperations.get_all_configs import get_all_configs
from CustomWidget.custom_button import CustomButtonPair
from CustomWidget.custom_label_pair import CustomLabelPair, CustomLabelPairEdit, CustomFilePicker, CustomLabelCheckbox, \
    CustomColorPicker
from CustomWidget.custom_separator import CustomSeparator


class SettingFrameEdit(Frame):
    """修改设置"""
    def __init__(self, master):
        self.root = master
        self.root.config()
        self.root.title("修改设置")
        # self.root.geometry("400x400")

        super(SettingFrameEdit, self).__init__(master)
        self.grid()
        self.create_widgets()
        self.new_data = {}

    def create_widgets(self):
        all_ini_data = get_all_configs()

        # self.version = CustomLabelPair(self, "版本", 1, label_data=all_ini_data['version'], right_col=1)
        # self.createtime = CustomLabelPair(self, "更新时间", 1, c=3, label_data=all_ini_data['createtime'], right_col=1)
        # CustomSeparator(self, 5)

        self.img_w = CustomLabelPairEdit(self, "W 宽px", 6, c=1, right_col=1, label_data=all_ini_data['img_w'])
        self.img_h = CustomLabelPairEdit(self, "H 高px", 6, c=3, right_col=1, label_data=all_ini_data['img_h'])
        self.img_type = CustomLabelCheckbox(self, "格式", 7, check_num=3, label_data=all_ini_data['check_list'])
        project_root = project_root_path()
        big_path = os.path.join(project_root, all_ini_data['big_dir'])
        self.big_img_dir = CustomFilePicker(self, "图库路径", 8, label_data=big_path)
        save_path = os.path.join(project_root, all_ini_data['small_dir'])
        self.save_img_dir = CustomFilePicker(self, "保存路径", 10, label_data=save_path)
        self.whether_add_border = CustomLabelCheckbox(self, "描边", 12, check_num=1, label_data=all_ini_data['whether_add_border'])
        self.border_color = CustomColorPicker(self, "描边颜色", 13, bg_color=all_ini_data['border_color'])
        self.border_width = CustomLabelPairEdit(self, "边宽px", 14, right_col=1, label_data=all_ini_data['border_width'])
        CustomSeparator(self, 20)

        self.font_size = CustomLabelPairEdit(self, "文字大小", 21, right_col=1, label_data=all_ini_data['font_size'])
        self.font_color = CustomColorPicker(self, "文字颜色", 22, bg_color=all_ini_data['font_color'])
        self.whether_add_text = CustomLabelCheckbox(self, "文字水印", 23, check_num=1, label_data=all_ini_data['whether_add_text'])
        CustomSeparator(self, 30)

        self.time_check = CustomLabelCheckbox(self, "文件名", 31, check_num=1, label_data=all_ini_data['timestamp'])

        l_function = self.change_setting_frame
        r_function = self.save_to_ini_file
        self.create_img_button = CustomButtonPair(self, 40, "取消", l_function, "保存", r_function)

    def save_to_ini_file(self):
        """保存修改数据到ini文件"""
        import os
        import configparser
        project_root = project_root_path()
        ini_file = os.path.join(project_root, "tool_setting.ini")
        conf = configparser.ConfigParser()
        conf.read(ini_file)

        conf['Image']['default_w'] = self.img_w.right_input.get()
        conf['Image']['default_h'] = self.img_h.right_input.get()
        conf['Image']['default_jpg'] = str(self.img_type.v1.get())
        conf['Image']['default_png'] = str(self.img_type.v2.get())
        conf['Image']['default_gif'] = str(self.img_type.v3.get())
        conf['Image']['big_dir'] = self.big_img_dir.choose_path()
        conf['Image']['save_dir'] = self.save_img_dir.choose_path()
        conf['Image']['whether_add_border'] = str(self.whether_add_border.v1.get())
        conf['Image']['border_width'] = self.border_width.right_input.get()
        conf['Image']['border_color'] = self.border_color.choose_color()

        conf['Font']['default_size'] = self.font_size.right_input.get()
        conf['Font']['default_color'] = self.font_color.choose_color()
        conf['Font']['whether_add_text'] = str(self.whether_add_text.v1.get())

        conf['Time']['whether_timestamp'] = str(self.time_check.v1.get())

        with open(ini_file, 'w') as ini_writer:
            conf.write(ini_writer)

        # 保存成功后，跳转设置显示窗口
        from PageFrames.setting_frame import SettingFrame

        self.destroy()
        SettingFrame(self.root)

    def change_setting_frame(self):
        """不保存，直接跳转回到设置展示页面"""
        from PageFrames.setting_frame import SettingFrame

        self.destroy()
        SettingFrame(self.root)






